﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ATA.Data.MisaDAL;
using Infragistics.Win.UltraWinDataSource;
using Infragistics.Win.UltraWinEditors;

namespace ATA.Winforms
{
    public partial class frmHotrovanchuyen : ATA.Winforms.frmChietkhau
    {
        public frmHotrovanchuyen()
        {
            InitializeComponent();
        }

        private void frmHotrovanchuyen_Load(object sender, EventArgs e)
        {
            foreach (AccountingObject v_lsParentCusomers in Globals.gv_lsCustomers)
            {
                UltraDataRow row = dsCustomers.Rows.Add();
                row["AccountingObjectId"] = v_lsParentCusomers.AccountingObjectId.ToString();
                row["ParentAccountingObjectId"] = v_lsParentCusomers.ParentAccountingObjectId.ToString();
                row["AccountingObjectName"] = v_lsParentCusomers.AccountingObjectName;
                row["AccountingObjectCode"] = v_lsParentCusomers.AccountingObjectCode;
                foreach (AccountingObject v_lsChildCusomers in Globals.gv_lsCustomersNotInMisa)
                {
                    if (v_lsChildCusomers.ParentAccountingObjectId.Equals(v_lsParentCusomers.AccountingObjectId))
                    {
                        UltraDataRowsCollection childRows = row.GetChildRows("Band 1");
                        UltraDataRow childRow = childRows.Add();
                        childRow["ParentAccountingObjectId"] = v_lsChildCusomers.ParentAccountingObjectId.ToString();
                        childRow["AccountingObjectId"] = v_lsChildCusomers.AccountingObjectId.ToString();
                        childRow["AccountingObjectName"] = v_lsChildCusomers.AccountingObjectName;
                        childRow["AccountingObjectCode"] = v_lsChildCusomers.AccountingObjectCode;
                    }
                }

            }

            grdResults.DataSource = dsCustomers;
            grdResults.DisplayLayout.Bands[0].Columns["AccountingObjectID"].Hidden = true;
            grdResults.DisplayLayout.Bands[0].Columns["ParentAccountingObjectID"].Hidden = true;
            grdResults.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Header.Caption = "Mã KH";
            grdResults.DisplayLayout.Bands[0].Columns["AccountingObjectCode"].Width = 120;
            grdResults.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Header.Caption = "Tên KH";
            grdResults.DisplayLayout.Bands[0].Columns["AccountingObjectName"].Width = 250;
            grdResults.DisplayLayout.Bands[1].Columns["AccountingObjectID"].Hidden = true;
            grdResults.DisplayLayout.Bands[1].Columns["ParentAccountingObjectID"].Hidden = true;
            grdResults.DisplayLayout.Bands[1].Columns["AccountingObjectCode"].Header.Caption = "Mã KH";
            grdResults.DisplayLayout.Bands[1].Columns["AccountingObjectCode"].Width = 120;
            grdResults.DisplayLayout.Bands[1].Columns["AccountingObjectName"].Header.Caption = "Tên KH";
            grdResults.DisplayLayout.Bands[1].Columns["AccountingObjectName"].Width = 250;
            grdResults.Width = 520;
            grdResults.DataBind();

            DropDownEditorButton objKhachHang = new DropDownEditorButton();
            objKhachHang.Control = this.grdResults;
            txtCustomers.ButtonsRight.Add(objKhachHang);
        }

        private void grdResults_DoubleClickRow(object sender, Infragistics.Win.UltraWinGrid.DoubleClickRowEventArgs e)
        {
            txtCustomers.Focus();
        }

        private void txtCustomers_AfterEditorButtonCloseUp(object sender, EditorButtonEventArgs e)
        {
            if (grdResults.ActiveRow.Index > -1)
            {
                if (grdResults.ActiveRow.Cells["AccountingObjectID"].Value != null)
                {
                    txtCustomers.Text = grdResults.ActiveRow.Cells["AccountingObjectCode"].Value.ToString();
                    txtCustomerName.Text = grdResults.ActiveRow.Cells["AccountingObjectName"].Value.ToString();
                }
            }
        }
    }
}
